<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Admin\Controller;

/**
 * Description of QuestionController
 *
 * @author gc
 */
class QuestionController extends AdminController
{

    protected $course = '';

    public function _initialize()
    {
        parent::_initialize();
        $cid = I("cid");
        if (!empty($cid)) {
            session('cid', $cid);
        } else {
            $cid = session('cid');
        }
        $this->course = D("Course")->getCourseById($cid);
    }

    public function index()
    {
        $map['course_id'] = $this->course['id'];
        $map['status'] = array('eq', 1);
        $question = I("question");
        if (!empty($question)) {
            $map['question'] = array('like', '%' . (string) $question . '%');
        }

        $list = $this->lists('Course_assessmont', $map, 'gorder asc');
        $this->assign('_list', $list);

        $this->meta_title = "《{$this->course['title']}》 学情评估题目列表";
        $this->display();
    }

    public function add()
    {
        $id = I("id");
        $meta_title = "《{$this->course['title']}》新增学情评估题目";
        if (!empty($id)) {
            $map["id"] = $id;
            $data = M("Course_assessmont")->where($map)->find();
            $this->assign("data", $data);
            $meta_title = "《{$this->course['title']}》编辑学情评估题目";
        }
        $this->meta_title = $meta_title;
        $this->display();
    }

    public function save()
    {
        $data = I("post.");
        $id = I("id");
        $data['course_id'] = $this->course['id'];
        if (empty($id)) {
            $data['status'] = 1;
            $data['create_time'] = $data['update_time'] = time();
            unset($data['id']);
        } else {
            $data['update_time'] = time();
        }

        $model = M("Course_assessmont");
        $s = empty($id) ? $model->add($data) : $model->save($data);
        $s ? $this->success("操作成功！", U("index")) : $this->error("操作失败！");
    }

    public function delete()
    {
        $ids = I("id");
        $map['id'] = array('in', $ids);

        $data = [
            'status' => -1,
            'update_time' => time()
        ];

        $s = M("Course_assessmont")->where($map)->save($data);
        $s ? $this->success("操作成功！", U("index")) : $this->error("操作失败！");
    }

}
